package com.bsj.sipgateway.task;

import com.bsj.power.common.def.entity.gb28181.Device;
import com.bsj.sipgateway.cache.DeviceCache;
import com.bsj.sipgateway.cache.ResetChannelCache;
import com.bsj.sipgateway.sip.transmit.cmd.impl.SIPCommander;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @Description 重新获取设备目录定时器
 * @Author zhaoyang
 * @Date 2024/6/18 9:32
 */
@EnableScheduling
@Configuration
@Slf4j
public class RefreshCatalogTask {

    @Autowired
    private SIPCommander cmder;

    @Value("${sip.cvrIds}")
    private String cvrIds;

    /**
     * 每隔10分钟执行一次。
     */
    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void refresh() {
//        String[] split = cvrIds.split(",");
//        for (String cvrId : split) {
//            Device device = DeviceCache.getDevice(cvrId);
//            if (device == null) {
//                log.info("定时任务下发catalogQuery时没有获取到对应的cvr的缓存信息，cvrId={}", cvrId);
//                continue;
//            }
//            cmder.catalogQuery(device);
//            //修改重置设备列表flag
//            ResetChannelCache.setResetFlag(device.getDeviceId(), 1);
//            log.info("请求获取cvr的catalog数据，cvrId={}", cvrId);
//        }


        List<Device> all = DeviceCache.getAll();
        for (Device device : all) {
            cmder.catalogQuery(device);
            //修改重置设备列表flag
            ResetChannelCache.setResetFlag(device.getDeviceId(), 1);
//            log.info("请求获取设备的catalog数据，deviceId={}", device.getDeviceId());
        }
    }


}
